package com.mq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ配置类
 */
@Configuration
public class RabbitConfig {

    @Autowired
    private MessageConverter messageConverter;

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(messageConverter);
        return rabbitTemplate;
    }

    // 定义消息队列
    @Bean
    public Queue ticketQueue() {
        return new Queue("ticketQueue", true);
    }

    @Bean
    public Queue OrderQueue() {
        return new Queue("OrderQueue", true);
    }

    @Bean
    public Queue userRegisterQueue() {
        return new Queue("userRegisterQueue", true);
    }

    @Bean
    public Queue userLoginQueue() {
        return new Queue("userLoginQueue", true);
    }

    @Bean
    public Queue travelRecommendationsQueue() {
        return new Queue("travelRecommendationsQueue", true);
    }

    @Bean
    public Queue flightsListQueue() {
        return new Queue("flightsListQueue", true);
    }

    @Bean
    public Queue lockOrderQueue() {
        return new Queue("lockOrderQueue", true);
    }

    @Bean
    public Queue cancelOrderQueue() {
        return new Queue("cancelOrderQueue", true);
    }

    @Bean
    public Queue payOrderQueue() {
        return new Queue("payOrderQueue", true);
    }

    @Bean
    public Queue orderListQueue() {
        return new Queue("orderListQueue", true);
    }

    // 定义交换机
    @Bean
    public DirectExchange exchange() {
        return new DirectExchange("MQ");
    }

    // 定义队列与交换机的绑定关系
    @Bean
    public Binding ticketBinding(Queue ticketQueue, DirectExchange exchange) {
        return BindingBuilder.bind(ticketQueue).to(exchange).with("ticketRoutingKey");
    }

    @Bean
    public Binding orderBinding(Queue OrderQueue, DirectExchange exchange) {
        return BindingBuilder.bind(OrderQueue).to(exchange).with("orderRoutingKey");
    }

    @Bean
    public Binding userRegisterBinding(Queue userRegisterQueue, DirectExchange exchange) {
        return BindingBuilder.bind(userRegisterQueue).to(exchange).with("userRegisterKey");
    }

    @Bean
    public Binding userLoginBinding(Queue userLoginQueue, DirectExchange exchange) {
        return BindingBuilder.bind(userLoginQueue).to(exchange).with("userLoginKey");
    }

    @Bean
    public Binding travelRecommendationsBinding(Queue travelRecommendationsQueue, DirectExchange exchange) {
        return BindingBuilder.bind(travelRecommendationsQueue).to(exchange).with("travelRecommendationsKey");
    }

    @Bean
    public Binding flightsListBinding(Queue flightsListQueue, DirectExchange exchange) {
        return BindingBuilder.bind(flightsListQueue).to(exchange).with("flightsListKey");
    }

    @Bean
    public Binding lockOrderBinding(Queue lockOrderQueue, DirectExchange exchange) {
        return BindingBuilder.bind(lockOrderQueue).to(exchange).with("lockOrderKey");
    }

    @Bean
    public Binding cancelOrderBinding(Queue cancelOrderQueue, DirectExchange exchange) {
        return BindingBuilder.bind(cancelOrderQueue).to(exchange).with("cancelOrderKey");
    }

    @Bean
    public Binding payOrderBinding(Queue payOrderQueue, DirectExchange exchange) {
        return BindingBuilder.bind(payOrderQueue).to(exchange).with("payOrderKey");
    }

    @Bean
    public Binding orderListBinding(Queue orderListQueue, DirectExchange exchange) {
        return BindingBuilder.bind(orderListQueue).to(exchange).with("orderListKey");
    }
}
